﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace translator
{
    /// <summary>
    /// StringEditWindow.xaml 的交互逻辑
    /// </summary>
    public partial class StringEditWindow : Window
    {
        public MainWindow mainWindow;
        public StringEditModel model;
        public StringEditWindow(MainWindow mainWindow)
        {
            this.mainWindow = mainWindow;
            this.model = new StringEditModel(this);
            InitializeComponent();
            this.sid = -1;
            this.Visibility = Visibility.Hidden;
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.DataContext = model;
            this.Cancel_Btn.DataContext = model;
            this.OK_Btn.DataContext = model;
            this.APITranslate_Btn.DataContext = model;
        }

        public int sid;
        private TranslateAPI ChosenAPI;       // 选择的翻译API(未实现)

        public void EditString(string hint,string original,string Translated,int sid)
        {
            this.Visibility = Visibility.Visible;
            this.Focus();
            this.WindowState = WindowState.Normal;      
            this.HintBox.Text = hint;
            this.OriginalBox.Text = original;
            this.TranslatedBox.Text = Translated;
            this.sid = sid;
            
            this.TranslatedBox.Focus();
            Keyboard.Focus(this.TranslatedBox);
            this.TranslatedBox.Select(0, this.TranslatedBox.Text.Length);
            FocusManager.SetFocusedElement(this, TranslatedBox);
        }
        


        


        public void ToHideWindow()       //关闭
        {
            this.HintBox.Text = "";
            this.OriginalBox.Text = "";
            this.TranslatedBox.Text = "";
            this.sid = -1;
            this.Visibility = Visibility.Hidden;
            mainWindow.ToFocusStringTable();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            this.mainWindow.stringEditWindow = null;
        }
    }

    public class StringEditModel
    {
        public StringEditCancelCommand StringEditCancelCommand { get; set; }
        public StringEditOKCommand StringEditOKCommand { get; set; }
        public AutoTranslateCommand AutoTranslateCommand { get; set; }

        public StringEditModel(StringEditWindow stringEditWindow)
        {
            StringEditCancelCommand = new StringEditCancelCommand(stringEditWindow);
            StringEditOKCommand = new StringEditOKCommand(stringEditWindow);
            AutoTranslateCommand = new AutoTranslateCommand(stringEditWindow);
        }
    }
    public class StringEditCancelCommand : ICommand
    {
        StringEditWindow StringEditWindow;
        public StringEditCancelCommand(StringEditWindow StringEditWindow)
        {
            this.StringEditWindow = StringEditWindow;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            StringEditWindow.ToHideWindow();
        }
    }
    public class StringEditOKCommand : ICommand
    {
        StringEditWindow StringEditWindow;
        public StringEditOKCommand(StringEditWindow StringEditWindow)
        {
            this.StringEditWindow = StringEditWindow;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            StringOK();
        }
        public void StringOK()      //OK
        {
            StringEditWindow.mainWindow.UpdateStringTranslated(StringEditWindow.sid, StringEditWindow.TranslatedBox.Text);
            StringEditWindow.ToHideWindow();

            

            
        }
    }
    public class AutoTranslateCommand : ICommand        //未实现   自动翻译功能
    {
        StringEditWindow StringEditWindow;
        public AutoTranslateCommand(StringEditWindow StringEditWindow)
        {
            this.StringEditWindow = StringEditWindow;
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            StringEditWindow.mainWindow.Log("尝试用API翻译,但API翻译还未实现");
        }

    }
}
